<template>
  <div class="match-detail">
    <div style="position: absolute;top: 1.7rem;width: 100%;">
      <teamTabs :slider="slide" v-on:change="slideNumChange"></teamTabs>
      <roundSwiper v-show="shows" :roundList="roundList" v-on:roundNum="roundChange" :types="type"></roundSwiper>
      <div style="padding: 0.2rem;" v-show="shows">
        <div v-if="get_check"><a class="color_a" target="_blank" :href="get_check_table">查看签表</a></div>
        <roundNumber :match="signForm" :roundNum="roundNum" :keys="keys" :types="type"></roundNumber>
        <roundForm :form="form" v-if="formshow"></roundForm>
      </div>
      <div v-show="!shows" style="padding: 0.2rem;">暂无数据</div>
    </div>
  </div>
</template>
<script>
  import tabs from "./components/tabs";
  import teamTabs from "./components/teamTabs";
  import roundSwiper from "./components/roundSwiper";
  import matchBattle from "../../../components/match/matchBattle";
  import matchEndSearch from "./components/matchEndSearch";
  import roundNumber from "./components/roundNumber";
  import roundForm from "./components/roundForm";
  export default {
    components: {
      tabs,
      teamTabs,
      roundSwiper,
      matchBattle,
      matchEndSearch,
      roundNumber,
      roundForm
    },
    data() {
      return {
        activeTab: 2,
        com_id: localStorage.getItem("index"),
        battleList: [],
        signForm: [],
        websock: null,
        group: "",
        name: "",
        match_date: "",
        slide: [], //模拟
        roundList: [],
        group_list: [],
        keys: [],
        title: this.$route.query.name,
        roundNum: 0,
        type: null,
        form: [],
        indexs: '',
        loop: [],
        num: 0,
        shows: true,
        formshow: true,
        tabList: ["进行中", "已结束", "未开始", "签表"],
        status: [2, 3, 0, 100],
        offset: 0,
        noDate: false,
        ongoingpage: 0,
        ws: null,
        get_check: false,
        get_check_table: null
      };
    },
    mounted() {
      this.match_list()
      this.$store.commit('SET_MSG', this.$route.query.id)
    },
    computed: {
      letter() {
        let letter = []
        for (let i = 0; i < 25; i++) {
          for (let x = 0; x < 26; x++) {
            letter.push(String.fromCharCode((65 + i + 1)) + String.fromCharCode((65 + x)));
          }
        }
        return letter
      },
      _letters() {
        let letters = []
        let letters_26 = []
        for (let i = 0; i < 26; i++) {
          letters_26.push(String.fromCharCode(65 + i))
        }
        for (let i = 0; i < 25; i++) {
          for (let x = 0; x < 26; x++) {
            letters.push(
              String.fromCharCode(65 + i + 1) + String.fromCharCode(65 + x)
            );
          }
        }
        return letters_26.concat(letters)
      },
    },
    methods: {
      getCheckTable(group_id) {
        var params = new URLSearchParams();
        params.append("group_id", group_id);
        this.$axios
          .post("/mobile/Matching/get_check_table", params)
          .then(res => {
            if (res.data.code == 2000) {
              this.get_check = true
              this.get_check_table = res.data.data[0]
            } else {
              this.get_check = false
            }
          })
          .catch(res => {
            //  console.log(res.msg);
          });
      },
      match_list() {
        var params = new URLSearchParams();
        params.append("com_id", localStorage.getItem("index"));
        this.$axios
          .post("/mobile/matching/search_list", params)
          .then(res => {
            if (res.data.code == 2000) {
              console.log(1);
              this.slide = res.data.data.group;
              this.type = this.slide[0].com_system; //默认
              this.indexs = res.data.data.group[0].group_id
              this.getCheckTable(res.data.data.group[0].group_id)
              this.signform(res.data.data.group[0].group_id, this.type);
            }
          })
          .catch(res => {
            //  console.log(res.msg);
          });
      },
      signform(group_id, type) {
        if (type == 3) {
          this.formshow = false;
          this.indexs = group_id;
          var arr = []
          let first_wheel = this.slide[this.num].first_wheel
          for (var i = 0; i < first_wheel; i++) {
            arr.push('第' + (i + 1) + '轮')
          }
          arr[arr.length - 2] = "半决赛"
          arr[arr.length - 1] = "决赛"
          this.roundList = arr
          this.sign_table(1)
        }

        if (type == 2) {
          this.indexs = group_id;
          this.roundForms(1, 'A')
          this.loop_platoon(1, 'A')
          var arr = []
          arr[arr.length] = "A组"
          this.roundList = arr
        }

        if (type == 1) {
          let second_order = 'A',
            stage = 1
          this.indexs = group_id;
          this.roundForms(stage, second_order)
          this.loop_platoon(stage, second_order)
          let arr = []
          let letter = [];
          let str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; //如果后期添加这里
          letter = str.split('').concat(this.letter)
          let first_num = this.slide[this.num].first_num
          let second_wheel = this.slide[this.num].second_wheel
          for (let i = 0; i < first_num; i++) {
            arr.push('第一阶段-' + letter[i] + '组')
          }
          for (let i = 0; i < second_wheel; i++) {
            arr.push('第二阶段-第' + (i + 1) + '轮')
          }
          this.roundList = arr
        }
        if (type == 4) {
          let second_order = 'A',
            stage = 1
          this.indexs = group_id;
          this.roundForms(stage, second_order)
          this.loop_platoon(stage, second_order)
          let arr = []
          let letter = [];
          let str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; //如果后期添加这里
          letter = str.split('').concat(this.letter)
          let first_wheel = this.slide[this.num].first_wheel
          let second_num = this.slide[this.num].second_num
          for (let i = 0; i < first_wheel; i++) {
            arr.push('第一阶段-' + letter[i] + '组')
          }
          for (let i = 0; i < second_num; i++) {
            arr.push('第二阶段-' + letter[i] + '组')
          }
          this.roundList = arr
        }
        if (type == 5) {
          this.formshow = false;
          let area = 1
          this.indexs = group_id;
          let arr = []
          let first_num = this.slide[this.num].first_num
          let first_wheel = this.slide[this.num].first_wheel
          for (let i = 0; i < first_num; i++) {
            arr.push((i + 1) + '区')
          }
          this.roundList = arr
          this.preliminaries(area)
        }
        if (type == 6) {
          this.formshow = false;
          let second_order = 'A',
            stage = 1,
            wheel = 1
          this.indexs = group_id;
          let arr = []
          let first_num = this.slide[this.num].first_num //组数
          let first_wheel = this.slide[this.num].first_wheel //轮数
          let second_num = this.slide[this.num].second_num
          for (let i = 0; i < first_num; i++) {
            for (let j = 0; j < first_wheel; j++) {
              arr.push(this._letters[i]+'组-第'+(j + 1)+'轮')
            }
          }
          this.roundList = arr
          this.lifting_loop(stage, second_order, wheel)
        }
        if (type == null) {
          this.shows = false
        } else {
          this.shows = true
        }
      },
      childByValue(childValue) {
        this.group = childValue.group_id;
        this.name = childValue.name;
        this.match_date = childValue.match_date;
        this.no_ongoing_match()
      },
      slideNumChange(slideNum) {
        this.num = slideNum.i;
        this.type = slideNum.type;
        this.indexs = slideNum.index;
        this.signform(slideNum.index, slideNum.type)
        this.roundNum = 0;
        this.formshow = true;
        if (slideNum.type == 3 || slideNum.type == 5 || slideNum.type == 6) {
          this.formshow = false;
        }
        this.getCheckTable(this.indexs)
      },
      roundChange(roundObj) {
        let stage = roundObj.round.indexOf('第一阶段') == -1 ? 2 : 1
        this.roundNum = roundObj.showIndex;
        let roundNum = roundObj.showIndex;
        let keys = [];
        let values = [];
        for (let key in this.group_list[roundNum]) {
          if (this.group_list[roundNum].hasOwnProperty(key) === true) {
            keys.push(key);
            values.push(this.group_list[roundNum][key]);
          }
        }
        if (this.type == 3) {
          let substr = roundObj.showIndex + 1
          this.sign_table(substr)
          this.formshow = false;
          this.signForm = this.group_list
        }
        if (this.type == 1) {
          let substr = stage == 2 ? roundObj.round.slice(-2, -1) : roundObj.round.split('-')[1]
          this.roundForms(stage, substr)
          this.loop_platoon(stage, substr)
        }
        if (this.type == 4) {
          let substr = stage == 2 ? roundObj.round.slice(-2, -1) : roundObj.round.split('-')[1]
          this.roundForms(stage, substr)
          this.loop_platoon(stage, substr)
        }
        if (this.type == 5) {
          let area = parseInt(roundObj.round)
          this.preliminaries(area)
        }
        if (this.type == 6) {
          let stage = 1
          let arr = roundObj.round.split('-')
          let wheel = parseInt(arr[1].replace(/[^0-9]/ig,""))
          let group = arr[0].substr(0, arr[0].length-1);
          this.lifting_loop(stage, group, wheel)
        }
      },
      roundForms(stage, substr) {
        var params = new URLSearchParams();
        params.append("com_id", localStorage.getItem("index"));
        params.append("group_id", this.indexs);
        params.append("stage", stage);
        params.append("second_order", substr);
        this.$axios
          .post("/mobile/v1/loop/loop_table", params)
          .then(res => {
            if (res.data.code == 200) {
              if (this.roundNum >= this.slide[this.num].first_num) {
                this.formshow = false;
              } else {
                this.formshow = true;
                this.form = res.data.data
              }

            }
          })
          .catch(res => {
            //  console.log(res.msg);
          });
      },
      loop_platoon(stage, substr) {
        var params = new URLSearchParams();
        params.append("com_id", localStorage.getItem("index"));
        params.append("group_id", this.indexs);
        params.append("stage", stage);
        params.append("second_order", substr);
        this.$axios
          .post("/mobile/v1/loop/loop_platoon", params)
          .then(res => {
            if (res.data.code == 200) {
              this.signForm = res.data.data
            }
          })
          .catch(res => {
            //  console.log(res.msg);
          });
      },
      sign_table(wheel) {
        var params = new URLSearchParams();
        params.append("group_id", this.indexs);
        params.append("wheel", wheel); //传轮次
        this.$axios
          .post("/mobile/v1/loop/sign_table", params)
          .then(res => {
            if (res.data.code == 200) {
              this.signForm = res.data.data
            }
          })
          .catch(res => {
            //  console.log(res.msg);
          });
      },
      preliminaries(area) {
        var params = new URLSearchParams();
        params.append("group_id", this.indexs);
        params.append("area", area);
        this.$axios
          .post("/mobile/v1/loop/primary_table", params)
          .then(res => {
            if (res.data.code == 200) {
              this.signForm = res.data.data
            } else {
              this.shows = false
            }
          })
          .catch(res => {
            //  console.log(res.msg);
          });
      },
      lifting_loop(stage, second_order, wheel){
        var params = new URLSearchParams();
        params.append("group_id", this.indexs);
        params.append("stage", stage);
        params.append("second_order", second_order);
        params.append("wheel", wheel);
        this.$axios
          .post("/mobile/v1/loop/lifting_loop", params)
          .then(res => {
            if (res.data.code == 2000) {
              this.signForm = res.data.data
            } else {
              this.shows = false
            }
          })
          .catch(res => {
            //  console.log(res.msg);
          });
      }
    }
  };
</script>
<style scoped>
  .color_a {
    color: red;
  }

  .match-detail {
    margin-bottom: 1.1rem;
  }

  .tabs {
    position: fixed;
    top: 45px;
    z-index: 9999;
    width: 100%;
  }
</style>
